<?php
/**
 * 门店结算
 * User: ws
 * Date: 2018/10/12
 * Time: 13:48
 */

namespace app\admin\controller;

use think\Db;
class Sett  extends Init
{
    function index(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            if(is_numeric(input('type')) === true){
                $where['pay_state'] = input('type');
            }
            $where['s.name'] = array('like','%'.trim($key).'%');
            $list = Db::table('je_settlement')->alias('t')->join('je_store s','t.store_id = s.store_id')->field('s.name,t.*')->order('settlement_id desc')->where($where)->paginate(input('limit'))->toArray();

            foreach ($list['data'] as $key=>$val){
                $list['data'][$key]['orderid'] = count(explode(',',$val['orderid']));
            }

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }
        return view('list');
    }

    function edit(){
        if(request()->isPost()){
            $data = $this->request->post();

            $fees = db('settlement')->where('settlement_id',$data['settlement_id'])->value('fees');

            if($data['pay_charge']>$fees){
                return json(array('code'=>0,'msg'=>'操作失败，付款金额不能大于实际结算价'));
            }
            if(empty($data['pay_img'])){
                return json(array('code'=>0,'msg'=>'操作失败，请上传付款凭证'));
            }

            $data['pay_img'] = implode(',',$data['pay_img']);
            $data['pay_state'] = 1;
            $data['admin_user_id'] =  $_SESSION['think']['admin_user']['admin_user_id'];
            $data['pay_time'] = time();

            $ru = Db::name('settlement')->update($data);
            if (!$ru) {
                return json(array('code'=>0,'msg'=>'付款失败'));
            }
            return json(array('code'=>200,'msg'=>'付款成功'));
        }

        $id = input('id');
        if(empty($id)){
            return json($result = ['code'=>0,'msg'=>'缺少参数']);
        }

        $info = Db::table('je_settlement')->alias('t')->join('je_store s','t.store_id = s.store_id')->field('s.name,s.username,s.bank,s.number,s.store_name,t.*')->order('settlement_id desc')->where("settlement_id = {$id}")->find();

        $orderid = explode(',',$info['orderid']);
        $info['orderid'] = count($orderid);

        foreach ($orderid as $key=>$val){
            $info['order'][$key] = Db::name('order')->where("order_id={$val}")->field('order_id,order_sn,add_time,confirm_time,type,end_time,overdue_price,abrasion_price,lease_end_time,lease_time,business_settlement_price')->find();
            $info['order'][$key]['goods'] = Db::table('je_order_goods')->alias('o')->join('je_goods g','o.goods_id = g.goods_id')->join('je_spec_goods_price s','o.goods_id = s.goods_id and o.spec_item = s.price_key')->field('o.rent,o.settlement,o.goods_price,o.goods_number,o.spec_item_key,g.name as gname,s.settlement_price')->where("order_id = {$val}")->select();
        }

        foreach ($info['order'] as $k=>$v){
            if($v['type']==1){
                $info['order'][$k]['lease_end_time'] = floor(((strtotime(date('Y-m-d',$v['lease_end_time']))+86400)-strtotime(date('Y-m-d',$v['lease_time'])))/86400);
            }
        }

        return view('edit',['list'=>$info]);
    }

    function see(){
        $id = input('id');
        if(empty($id)){
            return json($result = ['code'=>0,'msg'=>'缺少参数']);
        }

        $info = Db::table('je_settlement')->alias('t')->join('je_store s','t.store_id = s.store_id')->field('s.name,s.username,s.bank,s.number,s.store_name,t.*')->order('settlement_id desc')->where("settlement_id = {$id}")->find();

        $orderid = explode(',',$info['orderid']);
        $info['orderid'] = count($orderid);

        foreach ($orderid as $key=>$val){
            $info['order'][$key] = Db::name('order')->where("order_id={$val}")->field('order_id,order_sn,add_time,confirm_time,type,end_time,overdue_price,abrasion_price,lease_end_time,lease_time,business_settlement_price')->find();
            $info['order'][$key]['goods'] = Db::table('je_order_goods')->alias('o')->join('je_goods g','o.goods_id = g.goods_id')->join('je_spec_goods_price s','o.goods_id = s.goods_id and o.spec_item = s.price_key')->field('o.rent,o.settlement,o.goods_price,o.goods_number,o.spec_item_key,g.name as gname,s.settlement_price')->where("order_id = {$val}")->select();
        }

        foreach ($info['order'] as $k=>$v){
            if($v['type']==1){
                $info['order'][$k]['lease_end_time'] = floor(((strtotime(date('Y-m-d',$v['lease_end_time']))+86400)-strtotime(date('Y-m-d',$v['lease_time'])))/86400);
            }
        }
        $info['admin_user_id'] = Db::name('admin_user')->where("admin_user_id={$info['admin_user_id']}")->value('name');
        $info['pay_img'] = explode(',',$info['pay_img']);
        return view('details',['list'=>$info]);
    }
}